// directives/auth.js
let userPerms = ['user:insert', 'user:update', 'user:delete'] // 模拟权限（实际从登录后获取）

export function setPermissions(perms) {
  userPerms = Array.isArray(perms) ? perms : []
}

function hasPermission(perm) {
  if (!perm) return true
  return userPerms.includes(perm)
}

export const auth = {
  mounted(el, binding) {
    if (!hasPermission(binding.value)) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  },
  updated(el, binding) {
    if (binding.value !== binding.oldValue) {
      if (!hasPermission(binding.value)) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  }
}